<?php

namespace App\V3\V3_2_7\Controllers;

use App\V3\V3_2_7\Models\AdminModel;
use App\V3\V3_2_7\Models\FeedBackModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class FeedBackController
{
    /**
     * 列表
     * change 在 2021/11/30 16:28 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'pageSize' => 'required|integer',//页面大小
            'currentPage' => 'required|integer',//当前页
            'sartTime' => 'date',//开始时间
            'endTime' => 'date',//结束时间
            'tag' => 'integer',//标记：1-蓝 2-黄 3-绿 4-红
            'search' => 'string',//搜索内容(手机号或账号)
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $condition = [];
        //时间筛选
        if(isset($data['sartTime']) && !empty($data['sartTime']) && isset($data['endTime']) && !empty($data['endTime'])){
            $sartTime = strtotime($data['sartTime']);
            $endTime = strtotime($data['endTime']);
            if($sartTime<$endTime){
                $condition[] = ['created_at','>',$sartTime];
                $condition[] = ['created_at','<',$endTime];
            }
        }
        //标记筛选
        if(isset($data['tag']) && !empty($data['tag'])){
            $condition[] = ['tag','=',$data['tag']];
        }

        if(isset($data['search']) && !empty($data['search'])){
            $model = FeedBackModel::where($condition)
                ->orderBy('id', 'desc')
                ->orWhere('username',$data['search'])
                ->orWhere('mobile',$data['search'])
                ->paginate($data['pageSize'], ['*'], '', $data['currentPage']);
        }else{
            $model = FeedBackModel::where($condition)
                ->orderBy('id', 'desc')
                ->paginate($data['pageSize'], ['*'], '', $data['currentPage']);
        }
        //响应
        return success($model);
    }

    /**
     * 添加
     * change 在 2021/11/30 16:21 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'content' => 'required|string',//反馈内容
            'user_id' => 'required|integer',//当前用户id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $data['created_at'] = time();
        $data['mobile'] = AdminModel::where('id',$data['user_id'])->value('mobile');
        $data['nickname'] = AdminModel::where('id',$data['user_id'])->value('nickname');
        $data['username'] = AdminModel::where('id',$data['user_id'])->value('username');
        FeedBackModel::insert($data);
        return success();
    }

    /**
     * 编辑
     * change 在 2021/12/16 11:26 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' => 'required|integer',//主键id
            'remark' => 'string',//备注
            'tag' => 'integer',//标记：1-蓝 2-黄 3-绿 4-红
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        FeedBackModel::where('id',$data['id'])->update($data);
        return success();
    }

    /**
     * 删除
     * change 在 2021/12/16 13:52 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' => 'required|integer',//主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        FeedBackModel::destroy($data['id']);
        return success();
    }
}
